package com.Brewing.springboot.service.TpUsers;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.Brewing.springboot.entity.TpUsers.TpUsers;
import com.Brewing.springboot.entity.TpUsers.TpUsersExample;
import com.Brewing.springboot.entity.TpUsers.TpUsersExample.Criteria;
import com.Brewing.springboot.entity.TpUsersFans.TpUsersFans;
import com.Brewing.springboot.entity.TpUsersFans.TpUsersFansExample;
import com.Brewing.springboot.entity.TpUsersLikeVideos.TpUsersLikeVideos;
import com.Brewing.springboot.entity.TpUsersLikeVideos.TpUsersLikeVideosExample;
import com.Brewing.springboot.entity.TpUsersReport.TpUsersReport;
import com.Brewing.springboot.mapper.TpUsersFansMapper;
import com.Brewing.springboot.mapper.TpUsersLikeVideosMapper;
import com.Brewing.springboot.mapper.TpUsersMapper;
import com.Brewing.springboot.mapper.TpUsersReportMapper;

import tk.mybatis.mapper.entity.Example;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author  庄磊
 * @date	2018年12月3日 下午2:33:37 
 * @version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private TpUsersMapper tpUsersMapper;
	
	@Autowired
	private TpUsersLikeVideosMapper tpUsersLikeVideosMappers;
	
	@Autowired
	private TpUsersFansMapper tpUsersFansMappers;
	
	@Autowired
	private TpUsersReportMapper tpUsersReportMappers;
	
	@Autowired
	private Sid sid;
	
	@Transactional(propagation=Propagation.SUPPORTS)
	@Override
	public boolean queryUsernameIsExist(String username) {
		// TODO Auto-generated method stub
		TpUsers tpUsers = new TpUsers();
		tpUsers.setUsername(username);
		TpUsers result = tpUsersMapper.selectOne(tpUsers);
		return result == null ? false : true;
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void saveUser(TpUsers users) {
		// TODO Auto-generated method stub
		String userId = sid.nextShort();
		users.setId(userId);
		tpUsersMapper.insert(users);
	}

	@Override
	public TpUsers queryUserForLogin(String username, String password) {
		TpUsersExample tpUsersExample = new TpUsersExample();
		Criteria criteria = tpUsersExample.createCriteria();
		criteria.andUsernameEqualTo(username);
		criteria.andPasswordEqualTo(password);
		TpUsers userResult = tpUsersMapper.selectOneByExample(tpUsersExample);
		return userResult;
	}

	@Override
	public void updateUserInfo(TpUsers tpUsers) {
		TpUsersExample tpUsersExample=new TpUsersExample();
		Criteria criteria=tpUsersExample.createCriteria();
		criteria.andIdEqualTo(tpUsers.getId());
		tpUsersMapper.updateByExampleSelective(tpUsers, tpUsersExample);
	}
	
	@Transactional(propagation=Propagation.SUPPORTS)
	@Override
	public TpUsers queryUserInfo(String userId) {
		TpUsersExample tpUsersExample=new TpUsersExample();
		Criteria criteria=tpUsersExample.createCriteria();
		criteria.andIdEqualTo(userId);
		TpUsers users = tpUsersMapper.selectOneByExample(tpUsersExample);
		return users;
	}

	/**
	 * <p>Title: isUsersLikeVideo</p>
	 */ 
	@Transactional(propagation = Propagation.SUPPORTS)
	@Override
	public boolean isUserLikeVideo(String userId, String videoId) {
		// TODO Auto-generated method stub
		if (StringUtils.isBlank(userId) || StringUtils.isBlank(videoId)) {
			return false;
		}
		TpUsersLikeVideosExample example = new TpUsersLikeVideosExample();
		com.Brewing.springboot.entity.TpUsersLikeVideos.TpUsersLikeVideosExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andVideoIdEqualTo(videoId);
		List<TpUsersLikeVideos> list = tpUsersLikeVideosMappers.selectByExample(example);
		if(list != null && list.size()>0) {
			return true;
		}
		return false;
	}

	/**
	 * <p>Title: saveUserFanRelation</p>
	 */ 
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void saveUserFanRelation(String userId, String fanId) {
		// TODO Auto-generated method stub
		String relId= sid.nextShort();
		TpUsersFans usersFans = new TpUsersFans();
		usersFans.setId(relId);
		usersFans.setUserId(userId);
		usersFans.setFanId(fanId);
		
		tpUsersFansMappers.insert(usersFans);
		
		tpUsersMapper.addFansCount(userId);
		tpUsersMapper.addFollersCount(fanId);
		
	}

	/**
	 * <p>Title: deleteUserFanRelation</p>
	 */ 
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void deleteUserFanRelation(String userId, String fanId) {
		// TODO Auto-generated method stub
		TpUsersFansExample example = new TpUsersFansExample();
		com.Brewing.springboot.entity.TpUsersFans.TpUsersFansExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andFanIdEqualTo(fanId);
		
		tpUsersFansMappers.deleteByExample(example);
		tpUsersMapper.reduceFansCount(userId);
		tpUsersMapper.reduceFollersCount(fanId);
	}
	
	
	
	@Transactional(propagation = Propagation.REQUIRED)
	/**
	 * <p>Title: reportUser</p>
	 */ 
	@Override
	public void reportUser(TpUsersReport usersReport) {
		// TODO Auto-generated method stub
		String urId = sid.nextShort();
		usersReport.setId(urId);
		usersReport.setCreateDate(new Date());
		
		tpUsersReportMappers.insert(usersReport);
	}

	/**
	 * <p>Title: queryIfFollow</p>
	 */ 
	@Override
	public boolean queryIfFollow(String userId, String fanId) {
		// TODO Auto-generated method stub
		TpUsersFansExample example = new TpUsersFansExample();
		com.Brewing.springboot.entity.TpUsersFans.TpUsersFansExample.Criteria criteria = example.createCriteria();
		
		criteria.andUserIdEqualTo(userId);
		criteria.andFanIdEqualTo(fanId);
		
		List<TpUsersFans> list = tpUsersFansMappers.selectByExample(example);
		if(list !=null && !list.isEmpty() && list.size()>0) {
			return true;
		}
		return false;
	}

	/**
	 * <p>Title: saveUserInfo</p>
	 */ 
	@Override
	public void saveUserInfo(String userId,String nickname) {
		// TODO Auto-generated method stub
		System.out.println(userId);
		TpUsersExample tpUsersExample=new TpUsersExample();
		Criteria criteria=tpUsersExample.createCriteria();
		criteria.andIdEqualTo(userId);
		TpUsers result = tpUsersMapper.selectOneByExample(tpUsersExample);
		result.setNickname(nickname);
		tpUsersMapper.updateByExampleSelective(result, tpUsersExample);
	}

}
